<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>refs</title>
    <style>
      ul {
        height: 100px;
        width: 200px;
        background-color: antiquewhite;
        /* overflow: hidden; */
        overflow: auto;
      }
      li {
        height: 20px;
      }
    </style>
  </head>

  <body>
    <!-- <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
    </ul> -->
    <!--  -->
    <!-- 准备好一个“容器” -->
    <div id="test"></div>
    <div id="test2"></div>

    <!-- 引入react核心库 -->
    <!-- <script type="text/javascript" src="js/react.development.js"></script> -->
    <script src="./js/v16.14.0/react.development_v16.14.0.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <!-- <script type="text/javascript" src="js/react-dom.development.js"></script> -->
    <script src="./js/v16.14.0/react-dom.development_v16.14.0.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script type="text/javascript" src="js/babel.min.js"></script>
    <!-- <script src="./change_weather.js"></script> -->
  </body>
  <script src="./js/prop-types.js"></script>
  <script type="text/babel">
    class News extends React.Component {
      state = { newsArr: [] };
      getSnapshotBeforeUpdate() {
        return this.refs.listRef.scrollHeight;
      }
      // 接收getSnapshoteBeforeUpdate里面的信息
      componentDidUpdate(preProps, preState, height) {
        // 内容的高度和滚动的高度其实都是不断增加的
        // this.refs.listRef.scrollTop += height - this.refs.listRef.scrollHeight;
        // 前者新的新闻出现的高度,后者是之前的状态的高度
        console.log(this.refs.listRef.scrollHeight,'scrollHeight',height,'height');
        this.refs.listRef.scrollTop += this.refs.listRef.scrollHeight - height
      }
      //    已经出生钩子
      componentDidMount() {
        // 定时器模拟新闻
        setInterval(() => {
          // 先获取原来的状态
          const { newsArr } = this.state;
          let news = "新闻news里面的" + (newsArr.length + 1);
          // 新增一条新闻
          // 这样写不对,...必须放在[]里面
          // this.setState({newsArr:...news})
          // 展开运算符的妙用
          this.setState({ newsArr: [news, ...newsArr] });
        }, 1000);
      }
      render() {
        return (
          <ul ref="listRef">
            {this.state.newsArr.map((x, index) => {
              return <li key={index}>{x}</li>;
            })}
          </ul>
        );
      }
    }
    ReactDOM.render(<News />, document.getElementById("test"));
  </script>
</html>
